package com.dormitory.management.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.entity.Student;
import com.dormitory.management.system.req.*;
import com.dormitory.management.system.resp.RoomResp;
import com.dormitory.management.system.service.RoomService;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 房间表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "房间管理")
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    @ApiOperation("添加房间")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody RoomSaveReq req){
        if(!roomService.save(req)) {
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    @ApiOperation("批量添加房间")
    @RequestMapping(value = "/saveBatch",method = RequestMethod.POST)
    public Result<String> saveBatch(@RequestBody RoomSaveBatchReq req){
        if(!roomService.saveBatch(req)) {
            return Result.error("批量添加失败！");
        }
        return Result.success("批量添加成功！");
    }

    @ApiOperation("设置寝室长")
    @RequestMapping(value = "/saveLeader",method = RequestMethod.POST)
    public Result<String> saveLeader(@RequestBody RoomSaveLeaderReq req){
        if(!roomService.saveLeader(req)) {
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    @ApiOperation("删除房间")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(
            @ApiParam(value = "房间id",required = true)
            @PathVariable Long id){
        if(!roomService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("删除楼栋全部房间")
    @RequestMapping(value = "/deleteAll/{id}",method = RequestMethod.POST)
    public Result<String> deleteAll(@PathVariable Long id){
        if(!roomService.deleteAll(id)) {
            return Result.error("楼栋房间删除失败！");
        }
        return Result.success("楼栋房间删除成功！");
    }

    @ApiOperation("更新房间")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<String> update(@RequestBody RoomUpdateReq req){
        if(!roomService.update(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }



    @ApiOperation("房间分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<RoomResp>> getPage(@RequestBody RoomPageReq req){
        return Result.ok(roomService.getPage(req));
    }

    @ApiOperation("获取剩余人数房间列表")
    @RequestMapping(value = "/getList/{id}",method = RequestMethod.POST)
    public Result<List<RoomResp>> getList(@PathVariable Long id){
        return Result.ok(roomService.getList(id));
    }

    @ApiOperation("获取当前房间内的学生列表")
    @RequestMapping(value = "/getStuList/{id}",method = RequestMethod.POST)
    public Result<List<Student>> getStuList(@PathVariable Long id){
        return Result.ok(roomService.getStuList(id));
    }

}
